<?php

/**
 * Class Solution
 * 输入[4,3,2,1]输出[4,3,2,2]
 * 输入[4,8,9,9]输出[4,9,0,0]
 */
class Solution {

    /**
     * @param Integer[] $digits
     * @return Integer[]
     */
    public function plusOne($digits) {
        $len = count($digits);
        $str = "";
        $l = 0;
        for($i = $len - 1;$i >= 0;$i--){
            if($digits[$i] + 1 == 10){
                if($i > 0){
                    $str = "0".$str;
                }else{
                    $str = "10".$str;
                }

            }else{
                $str = ($digits[$i]+1).$str;
                $l = $i;
                break;
            }
        }
        for($l = $l-1;$l>=0;$l--){
            $str = $digits[$l].$str;
        }
        return str_split($str);
    }
}

$s = new Solution();
$input = [4,8,9,9];
$res = $s->plusOne($input);
print_r($res);
die();
